package rmi.dataSource;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.rmi.Remote;
import java.util.Iterator;
import java.util.Map;

import db.beans.SaeInfoBean;


import xml.XMLRmiInfo;

public class RunServer {

	@SuppressWarnings({ "unchecked", "rawtypes"})
	public static void main(String[] args) {

		String[] prompts = { "XML Dir:" };
		String xmlDir = "";

		if (args.length >= 1) {
			xmlDir = args[0];
		} else {
			InputStreamReader isr = new InputStreamReader(System.in);
			BufferedReader br = new BufferedReader(isr);

			int idx = 0;
			String sen = "";
			while (idx < 1) {
				try {
					System.out.print(prompts[idx]);
					sen = br.readLine();
					if (sen.trim().length() > 2) {
						switch (idx) {
						case 0:
							xmlDir = sen;
							break;
						}
						idx++;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		String className = "";
		String ip = "";
		String port = "";
		String name = "";
		String messageCenterRmi = "";
		String wakeUpTimeInterval ="";
		String studentMatchAlgorithm = "";

		XMLRmiInfo xmlRmiInfo = new XMLRmiInfo(xmlDir);
		Map<String, Map<String, String>> rmiInfo = xmlRmiInfo.getClassInfo();
		Iterator iterator = rmiInfo.entrySet().iterator();
		while (iterator.hasNext()) {
			try {
				Map.Entry entry = (Map.Entry) iterator.next();
				className = (String) entry.getKey();
				Map<String, String> classInfo = (Map<String, String>) entry
						.getValue();
				
				ip = classInfo.get("ip");
				port = classInfo.get("port");
				name = classInfo.get("name");
				
				messageCenterRmi = classInfo.get("messageCenterRmi");
				wakeUpTimeInterval = classInfo.get("wakeUpTimeInterval");
				studentMatchAlgorithm = classInfo.get("studentMatchAlgorithm");
				
				SaeInfoBean saeInfoBean = new SaeInfoBean();
				saeInfoBean.setBaseUrl(classInfo.get("connectURI"));
				saeInfoBean.setUsername(classInfo.get("username"));
				saeInfoBean.setPswd(classInfo.get("pswd"));
				saeInfoBean.setDriverClassName(classInfo.get("driverClass"));
				saeInfoBean.setInitialSize(classInfo.get("initialSize"));
				saeInfoBean.setMaxActive(classInfo.get("maxActive"));
				saeInfoBean.setMaxIdle(classInfo.get("maxIdle"));
				saeInfoBean.setMinIdle(classInfo.get("minIdle"));
				saeInfoBean.setMaxWait(classInfo.get("maxWait"));
				
				Class newClass = Class.forName(className);
				Constructor con = newClass.getConstructor(SaeInfoBean.class,String.class,String.class,String.class);
				Remote stub = (Remote) con.newInstance(saeInfoBean,messageCenterRmi,wakeUpTimeInterval,studentMatchAlgorithm);

				java.rmi.Naming.rebind("rmi://" + ip + ":" + port + "/" + name,
						stub);

				System.out.println(className + " is Ready on " + ip + ":"
						+ port + "/" + name + " " + new java.util.Date());
			} catch (Exception e) {
				e.printStackTrace(System.out);
				System.out.println(className + " " + ip + ":" + port + "/"
						+ name + " fail to start");
			}

		}
	}
	
}
